<!--
  * 数据源管理
  *
  * @Author:    shyu
  * @Date:      2024-09-27 14:08:09
  * @Copyright  shyu
-->
<template>
  <a-modal
      :title="form.connInformation ? '编辑' : '添加'"
      :width="600"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
        <a-form-item label="数据源类型"  name="data_source_type">
          <DictSelect width="100%" v-model:value="form.dataSourceType" keyCode="DATASOURCE_TYPE" placeholder="数据源类型"/>
        </a-form-item>
        <a-form-item label="数据源名称"  name="dataSourceName">
          <a-input style="width: 100%" v-model:value="form.dataSourceName" placeholder="数据源名称" />
        </a-form-item>
<!--        <a-form-item  label="连接信息"  name="connInformation">-->
<!--          <a-input style="width: 100%" v-model:value="form.connInformation" placeholder="连接信息" />-->
<!--        </a-form-item>-->
<!--        <a-form-item label="url"  name="url">-->
<!--          <a-input style="width: 100%" v-model:value="form.url" placeholder="url" />-->
<!--        </a-form-item>-->
<!--        <a-form-item label="用户名"  name="username">-->
<!--          <a-input style="width: 100%" v-model:value="form.username" placeholder="用户名" />-->
<!--        </a-form-item>-->
<!--        <a-form-item label="密码"  name="password">-->
<!--          <a-input style="width: 100%" v-model:value="form.password" placeholder="密码" />-->
<!--        </a-form-item>-->
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">下一步</a-button>
      </a-space>
    </template>
  </a-modal>
  <MySQLForm ref="mysqlRef" @reloadList="onEmit" />
</template>
<script setup>
  import { reactive, ref, nextTick,defineEmits  } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { datasourceApi } from '/@/api/business/super-computer/datasource/datasource-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import DictSelect from '/@/components/support/dict-select/index.vue';
  import MySQLForm from './MySQLConnect-modal.vue'

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    // console.log(rowData);
    Object.assign(form, rowData);
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    if (form.dataSourceType && form.dataSourceType.length > 0) {
      form.dataSourceType = form.dataSourceType.map((e) => e.valueCode);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      dataSourceId: undefined,
      dataSourceType: undefined, //数据源类型
      dataSourceName: undefined, //数据源名称
      connInformation: null, //连接信息
      // url: undefined, //数据源类型
      // username: undefined, //数据源名称
      // password: undefined, //连接信息
  };

  let form = reactive({ ...formDefault });

  
  const rules = {
      dataSourceType: [{ required: true, message: '数据源类型 必填' }],
      dataSourceName: [{ required: true, message: '数据源名称 必填' }],
      //connInformation: [{ required: true, message: '连接信息 必填' }],
  };

  const mysqlRef=ref();

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      //console.log(form);
      await formRef.value.validateFields();
      //save();
      if(form.dataSourceType=="MySQL"){
        // console.log(form);
        mysqlRef.value.showMySQL(form);
      }
      onClose();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.dataSourceId) {
        await datasourceApi.update(form);
      } else {
        await datasourceApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  function onEmit(){
    emits('reloadList');
  }



  defineExpose({
    show,
  });
</script>
